package toutiao.solution2018;

import java.util.*;

public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            map.put(sc.nextInt(),sc.nextInt());
        }
        // 可以使用内部类替换Map.Entry
        List<Map.Entry<Integer,Integer>> list = new ArrayList<>(map.entrySet());

        // Java8可以使用list.sort(new Comparator)替代
        Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                int x1= o1.getKey();
                int x2 = o2.getKey();
                return Integer.compare(x2, x1);
            }
        });
        int maxY = Integer.MIN_VALUE;
        int count = 0;
        List<Map.Entry<Integer,Integer>> list1 = new ArrayList<>();
        for (int i = 0;i < n; i++) {
            if (list.get(i).getValue() > maxY) {
                count++;
                list1.add(list.get(i));
                maxY = list.get(i).getValue();
            }
        }
        for (int i = count - 1; i >=0; i--) {
            System.out.println(list1.get(i).getKey()+" "+ list1.get(i).getValue());
        }
    }
}
